package caidan.net;

import caidan.res.Resource;
import jinyilw.common.TextTools;
import jinyilw.swing.visuals.Rcomp.RButton;
import jinyilw.swing.visuals.Rcomp.RDialog;
import jinyilw.swing.visuals.window.HintWindow;
import jinyilw.tools.h2db.DBConfig;
import jinyilw.tools.net.ClientTools;
import jinyilw.tools.net.NetConfig;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;

public class SetNetDialog extends RDialog
{
	private static final long serialVersionUID = 8445822534364913914L;
	private JTextField ipField, portField;
	private JButton setNetButton;
	private NetFunDialog netFunDialog;
	private ClientTools client;

	public SetNetDialog(ClientTools client, Window window)
	{
		super(window);
		this.client = client;
		init();
	}

	public SetNetDialog(NetFunDialog dialog)
	{
		super(dialog);
		netFunDialog = dialog;
		client = dialog.clientTools;
		init();
	}

	private void init()
	{
		setOptionStyle(150, 0.95f);
		setTitle("网络设置");
		setTitleHeight(30);
		setTitleColor(Color.green);
		setWindowSymbol(Resource.setNetIcon);
		setSection(10);
		setSize(300, 150);
		Container container = getContentPane();
		Container setContainer = new Container();
		container.add(setContainer, BorderLayout.CENTER);
		setContainer.setLayout(new GridLayout(0, 1, 0, 2));
		Container setIPPane = new Container();
		setIPPane.setLayout(new FlowLayout());
		setIPPane.add(new JLabel("服务器IP地址："));
		ipField = new JTextField(12);
		setIPPane.add(ipField);

		Container setPortPane = new Container();
		setPortPane.setLayout(new FlowLayout());
		setPortPane.add(new JLabel("服务器端口号："));
		portField = new JTextField(12);
		setPortPane.add(portField);

		setContainer.add(setIPPane);
		setContainer.add(setPortPane);

		setNetButton = new RButton();
		Container buttonContainer = new Container();
		buttonContainer.setLayout(new FlowLayout());
		buttonContainer.add(setNetButton);
		container.add(buttonContainer, BorderLayout.SOUTH);
		setNetButton.addActionListener((ActionEvent e) ->
		{
			setNetAct();
		});
		setResizable(false);
		update(client.isConnected());
		client.addReConnectRunner(() ->
		{
			update(true);
		});
		client.addBrokenRunner(() ->
		{
			update(false);
		});
	}

	public void setNetAct()
	{
		if (client.isConnected())
		{
			client.stop();
		} else
		{
			if (client.isConnecting())
				return;
			String serverIP = ipField.getText();
			int port = Integer.valueOf(portField.getText());
			if (!TextTools.isBlank(serverIP) && port > 1000 && port < 50000)
				new Thread(() ->
				{
					if (netFunDialog != null)
						netFunDialog.connecting();
					setNetButton.setEnabled(false);
					client.setParameter(serverIP, port);
					if (!client.isConnected())
						client.reConnect();
					if (client.isConnected())
					{
						DBConfig.setServerIP(serverIP);
						NetConfig.setServerIP(serverIP);
						NetConfig.setTcpServerPort(port);
					} else
						HintWindow.showHint("连接失败！", this);
					setNetButton.setEnabled(true);
					if (netFunDialog != null && !client.isConnected())
						netFunDialog.connectBroken();
				}).start();
		}
	}

	public void updateContent()
	{
		ipField.setText(client.getServerIP());
		portField.setText(String.valueOf(client.getServerPort()));
		//		if (client.isConnecting())
		//			setNetButton.setEnabled(false);
		// update(client.isConnected());
	}

	private void update(boolean b)
	{
		setNetButton.setEnabled(true);
		if (b)
		{
			ipField.setEditable(false);
			portField.setEditable(false);
			setNetButton.setText("断开连接");
			setNetButton.setIcon(Resource.disconnectIcon);
		} else
		{
			ipField.setEditable(true);
			portField.setEditable(true);
			setNetButton.setText("开始连接");
			setNetButton.setIcon(Resource.connectIcon);
		}
	}
}
